OpenRoads Designer CONNECT Edition SDK Help

Export all items type value in DGN into the report

The below code snippet shows reporting all the used item types for alignment, profile, and corridor into an excel file.


//Required References
using System;
using Bentley.DgnPlatformNET;
using System.Diagnostics;
using Bentley.CifNET.GeometryModel.SDK;
using System.Collections.Generic;
using Bentley.DgnPlatformNET.Elements;
using Bentley.CifNET.SDK;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using Bentley.CifNET.Formatting;
using System.IO;

private int nCnt = 1;

 public void ReportItemTypes()
        {
            try
            {
                //Create excel object to add data
                Excel.Application excelApp = new Excel.Application();
                if (excelApp != null)
                {
                    Excel.Workbook excelWorkbook = excelApp.Workbooks.Add("");
                    Excel.Worksheet excelWorksheet = excelApp.ActiveSheet;

                    //Get connection for edit purpose
                    ConsensusConnection sdkCon = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
                    if (sdkCon == null)
                        return;
                    //Get active Geometric model
                    GeometricModel geomModel = sdkCon.GetActiveGeometricModel();
                    if (geomModel == null)
                        return;

                    List<string> labels = new List<string>() { "Item Type Library Name", "Item Type Name", "Property Name", "Property Type", "Property Value" };

                    DgnModel dgn = geomModel.DgnModel;
                    DgnFile file = dgn.GetDgnFile();
                    Dictionary<string, string> fileProperties = new Dictionary<string, string>();

                    //Add current dgn details to report
                    excelWorksheet.Cells[nCnt, 1] = "Project";
                    excelWorksheet.Cells[nCnt, 2] = dgn.ModelName;
                    nCnt += 1;
                    excelWorksheet.Cells[nCnt, 1] = "File Name";
                    excelWorksheet.Cells[nCnt, 2] = file.GetFileName();
                    nCnt += 1;
                    excelWorksheet.Cells[nCnt, 1] = "Last Accessed";
                    excelWorksheet.Cells[nCnt, 2] = file.LastSaveTimeUtc.ToString();
                    nCnt += 1;

                    // Only Generate Header for Report
                    excelWorksheet.Cells[nCnt, 1] = "Horizontal Alignment Item Type Report";
                    nCnt += 1;

                    // Cycle through Horizontal Alignments 
                    foreach (Alignment al in geomModel.Alignments)
                    {
                        if (!al.IsFinalElement)
                            continue;

                        // Generate Header for alignment item types
                        string name = string.IsNullOrEmpty(al.Name) ? "Unnamed" : al.Name;

                        excelWorksheet.Cells[nCnt, 1] = "Alignment Name";
                        excelWorksheet.Cells[nCnt, 2] = name;
                        nCnt += 1;
                        excelWorksheet.Cells[nCnt, 1] = "ItemType Library Name";
                        excelWorksheet.Cells[nCnt, 2] = "ItemType Name";
                        excelWorksheet.Cells[nCnt, 3] = "Property Name";
                        excelWorksheet.Cells[nCnt, 4] = "Property Type";
                        excelWorksheet.Cells[nCnt, 5] = "Property Value";
                        nCnt += 1;

                        //Report item types used in alignments into excel
                        ReportItemTypesData(al.Element, ref excelWorksheet);
                    }

                    // Only Generate Header for Report
                    excelWorksheet.Cells[nCnt, 1] = "Profile Item Type Report";
                    nCnt += 1;

                    // Cycle through profiles
                    foreach (Alignment al in geomModel.Alignments)
                    {
                        if (!al.IsFinalElement)
                            continue;

                        foreach (Profile profile in al.Profiles)
                        {
                            if (!profile.IsFinalElement)
                                continue;

                            // Generate Header for profile item types
                            string name = string.IsNullOrEmpty(profile.Name) ? "Unnamed" : profile.Name;

                            excelWorksheet.Cells[nCnt, 1] = "Profile Name";
                            excelWorksheet.Cells[nCnt, 2] = name;
                            nCnt += 1;
                            excelWorksheet.Cells[nCnt, 1] = "ItemType Library Name";
                            excelWorksheet.Cells[nCnt, 2] = "ItemType Name";
                            excelWorksheet.Cells[nCnt, 3] = "Property Name";
                            excelWorksheet.Cells[nCnt, 4] = "Property Type";
                            excelWorksheet.Cells[nCnt, 5] = "Property Value";
                            nCnt += 1;

                            //Report item types used in profiles into excel
                            ReportItemTypesData(profile.Element, ref excelWorksheet);
                        }
                    }

                    //Report Header
                    excelWorksheet.Cells[nCnt, 1] = "Corridor Item Type Report";
                    nCnt += 1;

                    // Cycle through profiles
                    foreach (Corridor corridor in geomModel.Corridors)
                    {
                        // Generate Header for corridor item types
                        string name = string.IsNullOrEmpty(corridor.Name) ? "Unnamed" : corridor.Name;

                        excelWorksheet.Cells[nCnt, 1] = "Corridor Name";
                        excelWorksheet.Cells[nCnt, 2] = name;
                        nCnt += 1;
                        excelWorksheet.Cells[nCnt, 1] = "ItemType Library Name";
                        excelWorksheet.Cells[nCnt, 2] = "ItemType Name";
                        excelWorksheet.Cells[nCnt, 3] = "Property Name";
                        excelWorksheet.Cells[nCnt, 4] = "Property Type";
                        excelWorksheet.Cells[nCnt, 5] = "Property Value";
                        nCnt += 1;

                        //Report item types used in corridor into excel
                        ReportItemTypesData(corridor.Element, ref excelWorksheet);
                    }


                    //Save the data into .xlsx file 
                    string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                    filePath = Path.Combine(filePath, "Bentley");
                    filePath += "\\" + "ItemTypes" + ".xlsx";
                    excelApp.ActiveWorkbook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookDefault);

                    excelWorkbook.Close();
                    excelApp.Quit();

                    //Clean excel objects
                    FlushObject(excelWorksheet);
                    FlushObject(excelWorkbook);
                    FlushObject(excelApp);
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }



 //Helper function
        internal void ReportItemTypesData(Element element, ref Excel.Worksheet excelWorksheet)
        {
            CustomItemHost host = new CustomItemHost(element, true);
            if (host != null && host.CustomItemsCount > 0)
            {
                List<string> data = new List<string>();
                //Get all item types from current element
                foreach (Bentley.DgnPlatformNET.DgnEC.IDgnECInstance instance in host.CustomItems)
                {
                    excelWorksheet.Cells[nCnt, 1] = instance.ClassDefinition.Schema.NamespacePrefix.ToString();
                    excelWorksheet.Cells[nCnt, 2] = instance.ClassDefinition.Name.ToString();

                    //Iterate item type properties
                    foreach (Bentley.ECObjects.Instance.IECPropertyValue propertyValue in instance)
                    {
                        string propertyType = propertyValue.Type.DisplayLabel;

                        string value = "";
                        if (propertyValue is Bentley.ECObjects.Instance.IECArrayValue)
                        {
                            Bentley.ECObjects.Instance.IECArrayValue ecArray = propertyValue as Bentley.ECObjects.Instance.IECArrayValue;
                            foreach (Bentley.ECObjects.Instance.IECPropertyValue arrayValue in ecArray.ContainedValues)
                            {
                                if (arrayValue.ArrayIndex == 0)
                                {
                                    propertyType = "Array&lt;" + arrayValue.Type.DisplayLabel + "&gt;";
                                }
                                value += GetFormattedPropertyValue(arrayValue);
                                if (arrayValue.ArrayIndex < ecArray.Count - 1)
                                {
                                    value += "<br />";
                                }
                            }
                        }
                        else
                        {
                            value = GetFormattedPropertyValue(propertyValue);
                        }

                        excelWorksheet.Cells[nCnt + 1, 3] = propertyValue.AccessString.ToString();
                        excelWorksheet.Cells[nCnt + 1, 4] = propertyType.ToString();
                        excelWorksheet.Cells[nCnt + 1, 5] = value.ToString();

                        nCnt += 1;
                    }
                    nCnt += 1;
                }
                nCnt += 1;
            }
            else
            {
                excelWorksheet.Cells[nCnt, 1] = "No itemtypes to display.";
            }
        }
 //Helper function
        private static void FlushObject(Object _object)
        {
            try
            {
                if (_object != null)
                {
                    Marshal.ReleaseComObject(_object);
                    _object = null;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        //Helper function
        private string GetFormattedPropertyValue(Bentley.ECObjects.Instance.IECPropertyValue property)
        {
            if (Bentley.ECObjects.ECObjects.BooleanType == property.Type ||
                Bentley.ECObjects.ECObjects.IntegerType == property.Type ||
                Bentley.ECObjects.ECObjects.StringType == property.Type)
            {
                return property.StringValue;
            }
            else if (Bentley.ECObjects.ECObjects.DateTimeType == property.Type)
            {
                DateTime? dateTime = property.NativeValue as DateTime?;
                if (dateTime.HasValue)
                {
                    return dateTime.ToString();
                }
            }
            else if (Bentley.ECObjects.ECObjects.DoubleType == property.Type)
            {
                return FormatForDisplay.Double(property.DoubleValue);
            }
            else if (Bentley.ECObjects.ECObjects.PointType == property.Type)
            {
                Bentley.GeometryNET.DPoint3d? point = property.NativeValue as Bentley.GeometryNET.DPoint3d?;
                if (point.HasValue)
                {
                    return "[" + FormatForDisplay.Double(point.Value.X) + ", " + FormatForDisplay.Double(point.Value.Y) + "]";
                }
            }
            return string.Empty;
        }

Output